﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.BLL.Mtbl;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.App;
using System.Transactions;
using System.Text;
using System.Collections;

namespace WebApplication
{
    public partial class M_BranchOffice :BasePage
    {
        #region 定义静态变量存放网页刚加载时的信息
        static app_tblCust_Branch staticAppCustBranch;
        static app_tblContactInfo staticAppContactInfo;
        static app_tblAddress staticAppAddress;
        static string vidovalue = string.Empty;
        static string MedioRelated = string.Empty;
        #endregion

        protected string type = "";
        private string isfromApp = string.Empty;
        tblCustomer customerENT = null;
        int CustomerUID = 0;
        app_tblMediaRoomBLL app_mediaBLL = new app_tblMediaRoomBLL();
        tblMediaRoomBLL tbl_mediaBLL = new tblMediaRoomBLL();
        app_tblMediaRoom_RelatedBLL app_relatedBLL = new app_tblMediaRoom_RelatedBLL();
        tblMediaRoomRelatedBLL tbl_relatedBLL = new tblMediaRoomRelatedBLL();
        app_tblAddressBLL app_addressBLL = new app_tblAddressBLL();
        app_tblContactInfoBLL app_infoBLL = new app_tblContactInfoBLL();
        tblAddressBLL tbl_addressBLL = new tblAddressBLL();
        tblContactInfoBLL tbl_infoBLL = new tblContactInfoBLL();
        app_tblCust_BranchBLL app_cust_branchBLL = new app_tblCust_BranchBLL();
        tblCust_BranchBLL tbl_cust_branchBLL = new tblCust_BranchBLL();
        protected int ctuid = 0;
        int telNum = 5;
        int faxNum = 5;
        int emailNum = 5;
        int webNum = 2;
        int app_branchuid = 0;
        int tbl_branchuid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                customerENT = GetCustomerFromSession();
                if (customerENT != null)
                {
                    hidCustomerUID.Value = customerENT.CustomerUID.ToString();
                    CustomerUID = customerENT.CustomerUID;
                    InitPage();
                    
                    WriteStaticEnt();//初始化完成后,获取已经绑定好的数据到静态变量中
                    
                }
                else
                { CustomerNotLoginOption(); }
            }
        }
        protected void InitPage()
        {
            type = Request.QueryString["type"];
            BindArea();
            AddToDropList();
            BindCountry();
            initInfo();
            BindBranchList();
            BindBranchImgList();
            string strctuid = Request.QueryString[Constant.UrlParamName.custToContactPersonUID];
            string strfrom = Request.QueryString[Constant.UrlParamName.IsFromApp];
            if (strctuid != null && strfrom != null)
            {
                lkQuit.NavigateUrl = lkAdd.NavigateUrl = "/M_BranchOffice.aspx?type=add";
                int.TryParse(strctuid, out ctuid);
                if (ctuid > 0)
                {
                    if (Request.QueryString[Constant.UrlParamName.IsFromApp].ToLower() == "true")
                    {
                        SetFormFromApp(ctuid);
                    }
                    else if (Request.QueryString[Constant.UrlParamName.IsFromApp].ToLower() == "false")
                    {
                        app_tblCust_Branch app_cust_branch = new app_tblCust_Branch();
                        app_cust_branch.CustBranchUID = ctuid;
                        app_cust_branch.CustomerUID = CustomerUID;
                        app_cust_branch = app_cust_branchBLL.GetEntity(app_cust_branch,false);
                        if (app_cust_branch != null)
                        {
                            if (app_cust_branch.Deleted == 1)
                            {
                                SetNoOption(); lblMeg.Text = ResourceManage.GetResourceString("msg200014");
                            }
                            else
                            {
                                ctuid = app_cust_branch.AppCustBranchUID;
                                SetFormFromApp(ctuid);
                            }
                        }
                        else
                        { 
                            SetFormFromTbl(ctuid); 
                        }
                    }
                }
                else
                { SetNoOption(); lblMeg.Text = ResourceManage.GetResourceString("msg200001"); }
            }
        }

        private void SetFormFromApp(int ctuid)
        {
            app_tblCust_Branch app_cust_branch = new app_tblCust_Branch();
            app_cust_branch.AppCustBranchUID = ctuid;
            app_cust_branch.Deleted = 0;
            app_cust_branch.CustomerUID = CustomerUID;
            app_cust_branch = app_cust_branchBLL.GetEntity(app_cust_branch,true);
            if (app_cust_branch != null)
            {
                tbl_branchuid = app_cust_branch.CustBranchUID != null ? (int)app_cust_branch.CustBranchUID : 0;
                app_branchuid = app_cust_branch.AppCustBranchUID;
                txtSequenceNo.Text = app_cust_branch.SequenceNo!=null ? app_cust_branch.SequenceNo.ToString() : string.Empty;
                txtBranchName_Eng.Text = app_cust_branch.BranchName_Eng;
                txtBranchName_Chi.Text = app_cust_branch.BranchName_Chi;
                txtNoOfEmployee.Text = app_cust_branch.NOfEmp != null ? app_cust_branch.NOfEmp.ToString() : string.Empty;
                if (app_cust_branch.AppAddressUID > 0)
                {
                    app_tblAddress app_address = new app_tblAddress();
                    app_address.AppAddressUID = Convert.ToInt32(app_cust_branch.AppAddressUID);
                    app_address = app_addressBLL.GetAppENTByENT(app_address);
                    SetAddressFromApp(app_address);
                }
                if (app_cust_branch.AppContactInfoUID > 0)
                {
                    app_tblContactInfo app_info = new app_tblContactInfo();
                    app_info.AppContactInfoUID = Convert.ToInt32(app_cust_branch.AppContactInfoUID);
                    app_info = app_infoBLL.GetAppENTByENT(app_info);
                    SetInfoFromApp(app_info);
                }
                BindImgRelated(false);
            }
            else
            { SetNoOption(); lblMeg.Text = ResourceManage.GetResourceString("msg200014"); }
        }
        private void SetFormFromTbl(int ctuid)
        {
            tblCust_Branch tbl_cust_branch = new tblCust_Branch();
            tbl_cust_branch.CustBranchUID = ctuid;
            tbl_cust_branch.Deleted = 0;
            tbl_cust_branch.CustomerUID = CustomerUID;
            tbl_cust_branch = tbl_cust_branchBLL.GetEntity(tbl_cust_branch);
            if (tbl_cust_branch != null)
            {
                app_tblCust_Branch app_cust_branch = new app_tblCust_Branch();
                app_cust_branch.CustBranchUID = tbl_cust_branch.CustBranchUID;
                app_cust_branch.CustomerUID = CustomerUID;
                app_cust_branch = app_cust_branchBLL.GetEntity(app_cust_branch,false);
                if (app_cust_branch != null)
                {
                    Response.Redirect("/M_BranchOffice.aspx?ctcuid=" + app_cust_branch.AppCustBranchUID + "&isFromApp=True");
                    return;
                }

                tbl_branchuid = tbl_cust_branch.CustBranchUID;
                app_branchuid = 0;
                txtSequenceNo.Text = tbl_cust_branch.SequenceNo != null ? tbl_cust_branch.SequenceNo.ToString() : string.Empty;
                txtBranchName_Eng.Text = tbl_cust_branch.BranchName_Eng;
                txtBranchName_Chi.Text = tbl_cust_branch.BranchName_Chi;
                txtNoOfEmployee.Text = tbl_cust_branch.NOfEmp != null ? tbl_cust_branch.NOfEmp.ToString() : string.Empty;
                if (tbl_cust_branch.AddressUID > 0)
                {
                    tblAddress address = new tblAddress();
                    address.AddressUID = Convert.ToInt32(tbl_cust_branch.AddressUID);
                    address =tbl_addressBLL.GetEntity(address);
                    SetAddressFromTbl(address);
                }
                if (tbl_cust_branch.ContactInfoUID > 0)
                {
                    tblContactInfo info = new tblContactInfo();
                    info.ContactInfoUID = Convert.ToInt32(tbl_cust_branch.ContactInfoUID);
                    info = tbl_infoBLL.GetEntity(info);
                    SetInfoFromTbl(info);
                }
                BindImgRelated(false);
            }
            else
            { SetNoOption(); lblMeg.Text = ResourceManage.GetResourceString("msg200001"); }
        }
        protected void SetAddressFromApp(app_tblAddress ent)
        {
            if (ent != null)
            {
                hidAddressUID.Value = ent.AddressUID.ToString();
                txtArea_Chi.Text = ent.Area_Chi;
                txtArea_Eng.Text = ent.Area_Eng;
                txtCity_Chi.Text = ent.City_Chi;
                txtCity_Eng.Text = ent.City_Eng;
                if (ent.CountryUID != null)
                {
                    if (ent.mtblCountry.AreaUID != null)
                    {
                        ddlArea.SelectedValue = ent.mtblCountry.AreaUID.ToString();
                    }
                    BindCountry();
                    ddlCountry.SelectedValue = ent.CountryUID.ToString();

                }
                txtDistrict_Chi.Text = ent.District_Chi;
                txtDistrict_Eng.Text = ent.District_Eng;
                txtFlat_Chi.Text = ent.Flat_Bldg_Chi;
                txtFlat_Eng.Text = ent.Flat_Bldg_Eng;
                txtPostCode.Text = ent.PostalCode;
                txtProvince_Chi.Text = ent.Province_Chi;
                txtProvince_Eng.Text = ent.Province_Eng;
                txtStreet_Chi.Text = ent.Street_Rd_Chi;
                txtStreet_Eng.Text = ent.Street_Rd_Eng;
            }
        }
        protected void SetInfoFromApp(app_tblContactInfo ent)
        {
            if (ent != null)
            {
                hidInfoUID.Value = ent.ContactInfoUID.ToString();
                #region Info
                DropDownListTel1.SelectedValue = ent.Tel1_Type.ToString();
                TextBoxTC1.Text = ent.Tel1_CountryCode;
                TextBoxTA1.Text = ent.Tel1_AreaCode;
                TextBoxT1.Text = ent.Tel1;
                CheckBoxTD1.Checked = ent.Tel1_IsDNC == 1 ? true : false;
                CheckBoxTS1.Checked = ent.Tel1_IsSubscribe == 1 ? true : false;

                if (string.IsNullOrEmpty(ent.Tel1_CountryCode) && string.IsNullOrEmpty(ent.Tel1_AreaCode) && string.IsNullOrEmpty(ent.Tel1))
                {
                    PanelT1.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel2.SelectedValue = ent.Tel2_Type.ToString();
                TextBoxTC2.Text = ent.Tel2_CountryCode;
                TextBoxTA2.Text = ent.Tel2_AreaCode;
                TextBoxT2.Text = ent.Tel2;
                CheckBoxTD2.Checked = ent.Tel2_IsDNC == 1 ? true : false;
                CheckBoxTS2.Checked = ent.Tel2_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel2_CountryCode) && string.IsNullOrEmpty(ent.Tel2_AreaCode) && string.IsNullOrEmpty(ent.Tel2))
                {
                    PanelT2.Style.Add("display", "none");
                    telNum--;
                }



                DropDownListTel3.SelectedValue = ent.Tel3_Type.ToString();
                TextBoxTC3.Text = ent.Tel3_CountryCode;
                TextBoxTA3.Text = ent.Tel3_AreaCode;
                TextBoxT3.Text = ent.Tel3;
                CheckBoxTD3.Checked = ent.Tel3_IsDNC == 1 ? true : false;
                CheckBoxTS3.Checked = ent.Tel3_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel3_CountryCode) && string.IsNullOrEmpty(ent.Tel3_AreaCode) && string.IsNullOrEmpty(ent.Tel3))
                {
                    PanelT3.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel4.SelectedValue = ent.Tel4_Type.ToString();
                TextBoxTC4.Text = ent.Tel4_CountryCode;
                TextBoxTA4.Text = ent.Tel4_AreaCode;
                TextBoxT4.Text = ent.Tel4;
                CheckBoxTD4.Checked = ent.Tel4_IsDNC == 1 ? true : false;
                CheckBoxTS4.Checked = ent.Tel4_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel4_CountryCode) && string.IsNullOrEmpty(ent.Tel4_AreaCode) && string.IsNullOrEmpty(ent.Tel4))
                {
                    PanelT4.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel5.SelectedValue = ent.Tel5_Type.ToString();
                TextBoxTC5.Text = ent.Tel5_CountryCode;
                TextBoxTA5.Text = ent.Tel5_AreaCode;
                TextBoxT5.Text = ent.Tel5;
                CheckBoxTD5.Checked = ent.Tel5_IsDNC == 1 ? true : false;
                CheckBoxTS5.Checked = ent.Tel5_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel5_CountryCode) && string.IsNullOrEmpty(ent.Tel5_AreaCode) && string.IsNullOrEmpty(ent.Tel5))
                {
                    PanelT5.Style.Add("display", "none");
                    telNum--;
                }
                if (telNum <= 0)
                    PanelT5.Style.Add("display", "true");
                #endregion

                #region Fax
                //Fax
                DropDownListFax1.SelectedValue = ent.Fax1_Type.ToString();
                TextBoxFC1.Text = ent.Fax1_CountryCode;
                TextBoxFA1.Text = ent.Fax1_AreaCode;
                TextBoxF1.Text = ent.Fax1;
                CheckBoxFD1.Checked = ent.Fax1_IsDNC == 1 ? true : false;
                CheckBoxFS1.Checked = ent.Fax1_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax1) && string.IsNullOrEmpty(ent.Fax1_AreaCode) && string.IsNullOrEmpty(ent.Fax2_CountryCode))
                {
                    PanelF1.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax2.SelectedValue = ent.Fax2_Type.ToString();
                TextBoxFC2.Text = ent.Fax2_CountryCode;
                TextBoxFA2.Text = ent.Fax2_AreaCode;
                TextBoxF2.Text = ent.Fax2;
                CheckBoxFD2.Checked = ent.Fax2_IsDNC == 1 ? true : false;
                CheckBoxFS2.Checked = ent.Fax2_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax2) && string.IsNullOrEmpty(ent.Fax2_AreaCode) && string.IsNullOrEmpty(ent.Fax2_CountryCode))
                {
                    PanelF2.Style.Add("display", "none");
                    faxNum--;
                }




                DropDownListFax3.SelectedValue = ent.Fax3_Type.ToString();
                TextBoxFC3.Text = ent.Fax3_CountryCode;
                TextBoxFA3.Text = ent.Fax3_AreaCode;
                TextBoxF3.Text = ent.Fax3;
                CheckBoxFD3.Checked = ent.Fax3_IsDNC == 1 ? true : false;
                CheckBoxFS3.Checked = ent.Fax3_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax3) && string.IsNullOrEmpty(ent.Fax3_AreaCode) && string.IsNullOrEmpty(ent.Fax3_CountryCode))
                {
                    PanelF3.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax4.SelectedValue = ent.Fax4_Type.ToString();
                TextBoxFC4.Text = ent.Fax4_CountryCode;
                TextBoxFA4.Text = ent.Fax4_AreaCode;
                TextBoxF4.Text = ent.Fax4;
                CheckBoxFD4.Checked = ent.Fax4_IsDNC == 1 ? true : false;
                CheckBoxFS4.Checked = ent.Fax4_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax4) && string.IsNullOrEmpty(ent.Fax4_AreaCode) && string.IsNullOrEmpty(ent.Fax4_CountryCode))
                {
                    PanelF4.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax5.SelectedValue = ent.Fax5_Type.ToString();
                TextBoxFC5.Text = ent.Fax5_CountryCode;
                TextBoxFA5.Text = ent.Fax5_AreaCode;
                TextBoxF5.Text = ent.Fax5;
                CheckBoxFD5.Checked = ent.Fax5_IsDNC == 1 ? true : false;
                CheckBoxFS5.Checked = ent.Fax5_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax5) && string.IsNullOrEmpty(ent.Fax5_AreaCode) && string.IsNullOrEmpty(ent.Fax5_CountryCode))
                {
                    PanelF5.Style.Add("display", "none");
                    faxNum--;
                }
                if (faxNum <= 0)
                    PanelF1.Style.Add("display", "true");
                #endregion
                #region Email
                //email 
                DropDownListEm1.SelectedValue = ent.Email1_Type.ToString();
                TextBoxEmail1.Text = ent.Email1;
                CheckBoxES1.Checked = ent.Email1_IsSubscribe == 1 ? true : false;
                CheckBoxHB1.Checked = ent.Email1_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email1))
                {
                    PanelE5.Style.Add("display", "none");
                    emailNum--;
                }



                DropDownListEm2.SelectedValue = ent.Email2_Type.ToString();
                TextBoxEmail2.Text = ent.Email2;
                CheckBoxES2.Checked = ent.Email2_IsSubscribe == 1 ? true : false;
                CheckBoxHB2.Checked = ent.Email2_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email2))
                {
                    PanelE2.Style.Add("display", "none");
                    emailNum--;
                }

                DropDownListEm3.SelectedValue = ent.Email3_Type.ToString();
                TextBoxEmail3.Text = ent.Email3;
                CheckBoxES3.Checked = ent.Email3_IsSubscribe == 1 ? true : false;
                CheckBoxHB3.Checked = ent.Email3_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email3))
                {
                    PanelE3.Style.Add("display", "none");
                    emailNum--;
                }

                DropDownListEm4.SelectedValue = ent.Email4_Type.ToString();
                TextBoxEmail4.Text = ent.Email4;
                CheckBoxES4.Checked = ent.Email4_IsSubscribe == 1 ? true : false;
                CheckBoxHB4.Checked = ent.Email4_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email4))
                {
                    PanelE4.Style.Add("display", "none");
                    emailNum--;
                }


                DropDownListEm5.SelectedValue = ent.Email5_Type.ToString();
                TextBoxEmail5.Text = ent.Email5;
                CheckBoxES5.Checked = ent.Email5_IsSubscribe == 1 ? true : false;
                CheckBoxHB5.Checked = ent.Email5_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email5))
                {
                    PanelE5.Style.Add("display", "none");
                    emailNum--;
                }
                if (emailNum <= 0)
                    PanelE1.Style.Add("display", "true");
                #endregion
                #region WebSite
                //website
                DropDownListW1.SelectedValue = ent.Website1_Type.ToString();
                TextBoxWeb1.Text = ent.Website1;
                if (string.IsNullOrEmpty(ent.Website2))
                {
                    PanelW1.Style.Add("display", "none");
                    webNum--;
                }
                DropDownListW2.SelectedValue = ent.Website2_Type.ToString();
                TextBoxWeb2.Text = ent.Website2;
                if (string.IsNullOrEmpty(ent.Website2))
                {
                    PanelW2.Style.Add("display", "none");
                    webNum--;
                }
                if (webNum <= 0)
                    PanelW1.Style.Add("display", "true");
                #endregion
            }
            else
            {
                initInfo();
            }
        }
        protected void SetAddressFromTbl(tblAddress ent)
        {
            if (ent != null)
            {
                hidAddressUID.Value = ent.AddressUID.ToString();
                txtArea_Chi.Text = ent.Area_Chi;
                txtArea_Eng.Text = ent.Area_Eng;
                txtCity_Chi.Text = ent.City_Chi;
                txtCity_Eng.Text = ent.City_Eng;
                if (ent.CountryUID != null)
                {
                    if (ent.mtblCountry.AreaUID != null)
                    {
                        ddlArea.SelectedValue = ent.mtblCountry.AreaUID.ToString();
                    }
                    BindCountry();
                    ddlCountry.SelectedValue = ent.CountryUID.ToString();

                }
                txtDistrict_Chi.Text = ent.District_Chi;
                txtDistrict_Eng.Text = ent.District_Eng;
                txtFlat_Chi.Text = ent.Flat_Bldg_Chi;
                txtFlat_Eng.Text = ent.Flat_Bldg_Eng;
                txtPostCode.Text = ent.PostalCode;
                txtProvince_Chi.Text = ent.Province_Chi;
                txtProvince_Eng.Text = ent.Province_Eng;
                txtStreet_Chi.Text = ent.Street_Rd_Chi;
                txtStreet_Eng.Text = ent.Street_Rd_Eng;
            }
        }
        protected void SetInfoFromTbl(tblContactInfo ent)
        {
            if (ent != null)
            {
                hidInfoUID.Value = ent.ContactInfoUID.ToString();
                #region Info
                DropDownListTel1.SelectedValue = ent.Tel1_Type.ToString();
                TextBoxTC1.Text = ent.Tel1_CountryCode;
                TextBoxTA1.Text = ent.Tel1_AreaCode;
                TextBoxT1.Text = ent.Tel1;
                CheckBoxTD1.Checked = ent.Tel1_IsDNC == 1 ? true : false;
                CheckBoxTS1.Checked = ent.Tel1_IsSubscribe == 1 ? true : false;

                if (string.IsNullOrEmpty(ent.Tel1_CountryCode) && string.IsNullOrEmpty(ent.Tel1_AreaCode) && string.IsNullOrEmpty(ent.Tel1))
                {
                    PanelT1.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel2.SelectedValue = ent.Tel2_Type.ToString();
                TextBoxTC2.Text = ent.Tel2_CountryCode;
                TextBoxTA2.Text = ent.Tel2_AreaCode;
                TextBoxT2.Text = ent.Tel2;
                CheckBoxTD2.Checked = ent.Tel2_IsDNC == 1 ? true : false;
                CheckBoxTS2.Checked = ent.Tel2_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel2_CountryCode) && string.IsNullOrEmpty(ent.Tel2_AreaCode) && string.IsNullOrEmpty(ent.Tel2))
                {
                    PanelT2.Style.Add("display", "none");
                    telNum--;
                }



                DropDownListTel3.SelectedValue = ent.Tel3_Type.ToString();
                TextBoxTC3.Text = ent.Tel3_CountryCode;
                TextBoxTA3.Text = ent.Tel3_AreaCode;
                TextBoxT3.Text = ent.Tel3;
                CheckBoxTD3.Checked = ent.Tel3_IsDNC == 1 ? true : false;
                CheckBoxTS3.Checked = ent.Tel3_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel3_CountryCode) && string.IsNullOrEmpty(ent.Tel3_AreaCode) && string.IsNullOrEmpty(ent.Tel3))
                {
                    PanelT3.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel4.SelectedValue = ent.Tel4_Type.ToString();
                TextBoxTC4.Text = ent.Tel4_CountryCode;
                TextBoxTA4.Text = ent.Tel4_AreaCode;
                TextBoxT4.Text = ent.Tel4;
                CheckBoxTD4.Checked = ent.Tel4_IsDNC == 1 ? true : false;
                CheckBoxTS4.Checked = ent.Tel4_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel4_CountryCode) && string.IsNullOrEmpty(ent.Tel4_AreaCode) && string.IsNullOrEmpty(ent.Tel4))
                {
                    PanelT4.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel5.SelectedValue = ent.Tel5_Type.ToString();
                TextBoxTC5.Text = ent.Tel5_CountryCode;
                TextBoxTA5.Text = ent.Tel5_AreaCode;
                TextBoxT5.Text = ent.Tel5;
                CheckBoxTD5.Checked = ent.Tel5_IsDNC == 1 ? true : false;
                CheckBoxTS5.Checked = ent.Tel5_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel5_CountryCode) && string.IsNullOrEmpty(ent.Tel5_AreaCode) && string.IsNullOrEmpty(ent.Tel5))
                {
                    PanelT5.Style.Add("display", "none");
                    telNum--;
                }
                if (telNum <= 0)
                    PanelT5.Style.Add("display", "true");
                #endregion

                #region Fax
                //Fax
                DropDownListFax1.SelectedValue = ent.Fax1_Type.ToString();
                TextBoxFC1.Text = ent.Fax1_CountryCode;
                TextBoxFA1.Text = ent.Fax1_AreaCode;
                TextBoxF1.Text = ent.Fax1;
                CheckBoxFD1.Checked = ent.Fax1_IsDNC == 1 ? true : false;
                CheckBoxFS1.Checked = ent.Fax1_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax1) && string.IsNullOrEmpty(ent.Fax1_AreaCode) && string.IsNullOrEmpty(ent.Fax2_CountryCode))
                {
                    PanelF1.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax2.SelectedValue = ent.Fax2_Type.ToString();
                TextBoxFC2.Text = ent.Fax2_CountryCode;
                TextBoxFA2.Text = ent.Fax2_AreaCode;
                TextBoxF2.Text = ent.Fax2;
                CheckBoxFD2.Checked = ent.Fax2_IsDNC == 1 ? true : false;
                CheckBoxFS2.Checked = ent.Fax2_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax2) && string.IsNullOrEmpty(ent.Fax2_AreaCode) && string.IsNullOrEmpty(ent.Fax2_CountryCode))
                {
                    PanelF2.Style.Add("display", "none");
                    faxNum--;
                }




                DropDownListFax3.SelectedValue = ent.Fax3_Type.ToString();
                TextBoxFC3.Text = ent.Fax3_CountryCode;
                TextBoxFA3.Text = ent.Fax3_AreaCode;
                TextBoxF3.Text = ent.Fax3;
                CheckBoxFD3.Checked = ent.Fax3_IsDNC == 1 ? true : false;
                CheckBoxFS3.Checked = ent.Fax3_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax3) && string.IsNullOrEmpty(ent.Fax3_AreaCode) && string.IsNullOrEmpty(ent.Fax3_CountryCode))
                {
                    PanelF3.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax4.SelectedValue = ent.Fax4_Type.ToString();
                TextBoxFC4.Text = ent.Fax4_CountryCode;
                TextBoxFA4.Text = ent.Fax4_AreaCode;
                TextBoxF4.Text = ent.Fax4;
                CheckBoxFD4.Checked = ent.Fax4_IsDNC == 1 ? true : false;
                CheckBoxFS4.Checked = ent.Fax4_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax4) && string.IsNullOrEmpty(ent.Fax4_AreaCode) && string.IsNullOrEmpty(ent.Fax4_CountryCode))
                {
                    PanelF4.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax5.SelectedValue = ent.Fax5_Type.ToString();
                TextBoxFC5.Text = ent.Fax5_CountryCode;
                TextBoxFA5.Text = ent.Fax5_AreaCode;
                TextBoxF5.Text = ent.Fax5;
                CheckBoxFD5.Checked = ent.Fax5_IsDNC == 1 ? true : false;
                CheckBoxFS5.Checked = ent.Fax5_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax5) && string.IsNullOrEmpty(ent.Fax5_AreaCode) && string.IsNullOrEmpty(ent.Fax5_CountryCode))
                {
                    PanelF5.Style.Add("display", "none");
                    faxNum--;
                }
                if (faxNum <= 0)
                    PanelF1.Style.Add("display", "true");
                #endregion
                #region Email
                //email 
                DropDownListEm1.SelectedValue = ent.Email1_Type.ToString();
                TextBoxEmail1.Text = ent.Email1;
                CheckBoxES1.Checked = ent.Email1_IsSubscribe == 1 ? true : false;
                CheckBoxHB1.Checked = ent.Email1_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email1))
                {
                    PanelE5.Style.Add("display", "none");
                    emailNum--;
                }



                DropDownListEm2.SelectedValue = ent.Email2_Type.ToString();
                TextBoxEmail2.Text = ent.Email2;
                CheckBoxES2.Checked = ent.Email2_IsSubscribe == 1 ? true : false;
                CheckBoxHB2.Checked = ent.Email2_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email2))
                {
                    PanelE2.Style.Add("display", "none");
                    emailNum--;
                }

                DropDownListEm3.SelectedValue = ent.Email3_Type.ToString();
                TextBoxEmail3.Text = ent.Email3;
                CheckBoxES3.Checked = ent.Email3_IsSubscribe == 1 ? true : false;
                CheckBoxHB3.Checked = ent.Email3_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email3))
                {
                    PanelE3.Style.Add("display", "none");
                    emailNum--;
                }

                DropDownListEm4.SelectedValue = ent.Email4_Type.ToString();
                TextBoxEmail4.Text = ent.Email4;
                CheckBoxES4.Checked = ent.Email4_IsSubscribe == 1 ? true : false;
                CheckBoxHB4.Checked = ent.Email4_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email4))
                {
                    PanelE4.Style.Add("display", "none");
                    emailNum--;
                }


                DropDownListEm5.SelectedValue = ent.Email5_Type.ToString();
                TextBoxEmail5.Text = ent.Email5;
                CheckBoxES5.Checked = ent.Email5_IsSubscribe == 1 ? true : false;
                CheckBoxHB5.Checked = ent.Email5_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email5))
                {
                    PanelE5.Style.Add("display", "none");
                    emailNum--;
                }
                if (emailNum <= 0)
                    PanelE1.Style.Add("display", "true");
                #endregion
                #region WebSite
                //website
                DropDownListW1.SelectedValue = ent.Website1_Type.ToString();
                TextBoxWeb1.Text = ent.Website1;
                if (string.IsNullOrEmpty(ent.Website2))
                {
                    PanelW1.Style.Add("display", "none");
                    webNum--;
                }
                DropDownListW2.SelectedValue = ent.Website2_Type.ToString();
                TextBoxWeb2.Text = ent.Website2;
                if (string.IsNullOrEmpty(ent.Website2))
                {
                    PanelW2.Style.Add("display", "none");
                    webNum--;
                }
                if (webNum <= 0)
                    PanelW1.Style.Add("display", "true");
                #endregion
            }
            else
            {
                initInfo();
            }
        }
        private void SetNoOption()
        {
            initInfo();
            hidAllowToOption.Value = "false";
            lkbtnSave.Enabled = false;
        }
        private void AddToDropList()
        {
            Hashtable tellHas = Constant.InfoType.tellType;
            Hashtable FaxHas = Constant.InfoType.FaxType;
            Hashtable MailHas = Constant.InfoType.EmailType;
            Hashtable WebHas = Constant.InfoType.WebType;
            //telphone
            DropDownListTel1.DataSource = DropDownListTel2.DataSource = DropDownListTel3.DataSource = DropDownListTel4.DataSource = DropDownListTel5.DataSource = tellHas;
            DropDownListTel1.DataTextField = DropDownListTel2.DataTextField = DropDownListTel3.DataTextField = DropDownListTel4.DataTextField = DropDownListTel5.DataTextField = "value";
            DropDownListTel1.DataValueField = DropDownListTel2.DataValueField = DropDownListTel3.DataValueField = DropDownListTel4.DataValueField = DropDownListTel5.DataValueField = "key";
            DropDownListTel1.DataBind();
            DropDownListTel2.DataBind();
            DropDownListTel3.DataBind();
            DropDownListTel4.DataBind();
            DropDownListTel5.DataBind();
            //Fax
            DropDownListFax1.DataSource = DropDownListFax2.DataSource = DropDownListFax3.DataSource = DropDownListFax4.DataSource = DropDownListFax5.DataSource = FaxHas;
            DropDownListFax1.DataTextField = DropDownListFax2.DataTextField = DropDownListFax3.DataTextField = DropDownListFax4.DataTextField = DropDownListFax5.DataTextField = "value";
            DropDownListFax1.DataValueField = DropDownListFax2.DataValueField = DropDownListFax3.DataValueField = DropDownListFax4.DataValueField = DropDownListFax5.DataValueField = "key";
            DropDownListFax1.DataBind();
            DropDownListFax2.DataBind();
            DropDownListFax3.DataBind();
            DropDownListFax4.DataBind();
            DropDownListFax5.DataBind();
            //Mail
            DropDownListEm1.DataSource = DropDownListEm2.DataSource = DropDownListEm3.DataSource = DropDownListEm4.DataSource = DropDownListEm5.DataSource = MailHas;
            DropDownListEm1.DataTextField = DropDownListEm2.DataTextField = DropDownListEm3.DataTextField = DropDownListEm4.DataTextField = DropDownListEm5.DataTextField = "value";
            DropDownListEm1.DataValueField = DropDownListEm2.DataValueField = DropDownListEm3.DataValueField = DropDownListEm4.DataValueField = DropDownListEm5.DataValueField = "key";
            DropDownListEm1.DataBind();
            DropDownListEm2.DataBind();
            DropDownListEm3.DataBind();
            DropDownListEm4.DataBind();
            DropDownListEm5.DataBind();
            //WEBsit
            DropDownListW1.DataSource = DropDownListW2.DataSource = WebHas;
            DropDownListW1.DataTextField = DropDownListW2.DataTextField = "value";
            DropDownListW1.DataValueField = DropDownListW2.DataValueField = "key";
            DropDownListW1.DataBind();
            DropDownListW2.DataBind();
        }
        /// 新添加时 Contact Information 初始化
        private void initInfo()
        {
            //telephone
            PanelT1.Visible = true;
            PanelT2.Style.Add("display", "none");
            PanelT3.Style.Add("display", "none");
            PanelT4.Style.Add("display", "none");
            PanelT5.Style.Add("display", "none");

            //fax
            PanelF1.Visible = true;
            PanelF2.Style.Add("display", "none");
            PanelF3.Style.Add("display", "none");
            PanelF4.Style.Add("display", "none");
            PanelF5.Style.Add("display", "none");

            //email
            PanelE1.Visible = true;
            PanelE2.Style.Add("display", "none");
            PanelE3.Style.Add("display", "none");
            PanelE4.Style.Add("display", "none");
            PanelE5.Style.Add("display", "none");

            //website
            PanelW1.Visible = true;
            PanelW2.Style.Add("display", "none");


        }
        protected void BindArea()
        {
            mtblArea areaENT = new mtblArea();
            areaENT.Deleted = 0;
            mtblAreaBLL areaBLL = new mtblAreaBLL();
            var list = areaBLL.GetListByENT(areaENT);
            ddlArea.DataSource = list;
            ddlArea.DataTextField = "Area_Chi";
            ddlArea.DataValueField = "AreaUID";
            ddlArea.DataBind();
            BindCountry();
        }
        protected void BindCountry()
        {
            mtblCountry countryENT = new mtblCountry();
            mtblCountryBLL countryBLL = new mtblCountryBLL();
            countryENT.Deleted = 0;
            int areaUID = Convert.ToInt32(ddlArea.SelectedValue);
            countryENT.AreaUID = areaUID;
            var list = countryBLL.GetListByENT(countryENT);
            if (list.Count > 0)
            {
                ddlCountry.DataSource = list;
                ddlCountry.DataTextField = "Country_Chi";
                ddlCountry.DataValueField = "CountryUID";
                ddlCountry.DataBind();
            }
        }
        protected void BindBranchList()
        {
            var app_cust_branchAll = customerENT.app_tblCust_Branch; //查询Member对Customer的CP所有操作的所有记录
            customerENT.app_tblCust_Branch.Load();
            //var cust_cpAll = customerENT.tblCust_ContactPerson.Where(a => a.Deleted == 0).ToList(); //查询 Customer的CP所有未被删除的记录
            List<int> withOutcpList = new List<int>();
            //遍历被Memeber操作的数据，循环出tbl中的数据； 即：app表除新增数据外的其他记录
            foreach (var temp in app_cust_branchAll.Where(a => a.CustBranchUID > 0).ToList())
            {
                withOutcpList.Add((int)temp.CustBranchUID);
            }
            List<M_BranchListDetail> listValue = new List<M_BranchListDetail>();
            List<M_BranchListDetail> tblData = tbl_cust_branchBLL.GetListByCustUID(withOutcpList, customerENT.CustomerUID);
            List<M_BranchListDetail> appData = app_cust_branchBLL.GetListByCustUID(customerENT.CustomerUID);
            listValue.AddRange(tblData);
            listValue.AddRange(appData);
            rptBranchOfficeList.DataSource = listValue;
            rptBranchOfficeList.DataBind();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "AverageBrand();", true);
        }
        protected void BindBranchImgList()
        {
            app_tblMediaRoom app_media = new app_tblMediaRoom();
            app_media.CustomerUID = CustomerUID;
            app_media.MediaType = Constant.MediaType.image;
            app_media.Category = Constant.MediaRoomCategory.branch_image;
            var appALLList = app_mediaBLL.GetList(app_media, false);
            List<int> withOutList = new List<int>();
            foreach (var temp in appALLList.Where(a => a.TblKeyUID > 0).ToList())
            {
                withOutList.Add(temp.TblKeyUID);
            }
            var appList = appALLList.Where(a=>a.IsDeleted==0).ToList();

            tblMediaRoom mediaroomENT = new tblMediaRoom();
            tblMediaRoomBLL mediaroomBLL = new tblMediaRoomBLL();
            mediaroomENT.CustomerUID = Convert.ToInt32(CustomerUID);
            mediaroomENT.MediaType = Constant.MediaType.image;
            mediaroomENT.Category = Constant.MediaRoomCategory.branch_image;
            var tblList = mediaroomBLL.GetList(mediaroomENT, withOutList);
            var ListSource = new List<MediaRoomList>();
            ListSource.AddRange(appList);
            ListSource.AddRange(tblList);
            rptImgList.DataSource = ListSource;
            rptImgList.DataBind();
        }
        protected void BindImgRelated(bool isPostBack)
        {
            List<M_MediaRoomRelatedDetail> appAllList = new List<M_MediaRoomRelatedDetail>();
            List<M_MediaRoomRelatedDetail> listValue = new List<M_MediaRoomRelatedDetail>();
            List<M_MediaRoomRelatedDetail> appList = new List<M_MediaRoomRelatedDetail>();
            List<M_MediaRoomRelatedDetail> tblList = new List<M_MediaRoomRelatedDetail>();
            List<int> withOutList = new List<int>();
                //TODO 检查 appuid为空时，是否取全部图片出来了； 其他页面一样
                if (app_branchuid > 0)
                {
                    app_tblMediaRoom_Related app_related = new app_tblMediaRoom_Related();
                    app_tblMediaRoom_RelatedBLL app_relatedBLL = new app_tblMediaRoom_RelatedBLL();
                    if (app_branchuid > 0)
                        app_related.AppRelatedUID = app_branchuid;
                    app_related.Category = Constant.MediaRoomCategory.branch_image;
                    appAllList = app_relatedBLL.GetList(app_related, false);
                    appList = appAllList.Where(a => a.IsDeleted == 0).ToList();
                    foreach (var temp in appAllList.Where(a => a.TblMediaRelatedUID > 0).ToList())
                    {
                        withOutList.Add(temp.TblMediaRelatedUID);
                    }
                }
                if (tbl_branchuid > 0)
                {
                    tblMediaRoom_Related tbl_related = new tblMediaRoom_Related();
                    tbl_related.RelatedUID = tbl_branchuid;
                    tbl_related.Category = Constant.MediaRoomCategory.branch_image;
                    tblMediaRoomRelatedBLL tbl_relatedBLL = new tblMediaRoomRelatedBLL();
                    tblList = tbl_relatedBLL.GetList(tbl_related, withOutList);
                }
            listValue.AddRange(appList);
            listValue.AddRange(tblList); 
            if (!isPostBack)
            {
                rptRelatedImgList.DataSource = listValue;
                rptRelatedImgList.DataBind();
            }
            if (listValue.Count > 0)
            {
                var count = listValue.Count;
                for (int i = 0; i < count; i++)
                {
                    //记录原始已经关联的MediaRoomUID, 方便Save事件过滤掉 新增关联MediaRoomUID 确未保存到数据库 的记录
                    hidRelatedMediastrUID.Value += listValue[i].KeyMediaUID.ToString() + ",";
                }
            }
        }

        protected void btnRefreshImgList_Click(object sender, EventArgs e)
        {
            var tempent=GetCustomerFromSession();
            if (tempent != null)
            {
                customerENT = tempent;
                BindImgRelated(false);
            }
        }
        protected void btnRefreshBranchList_Click(object sender, EventArgs e)
        {
            var tempent=GetCustomerFromSession();
            if (tempent != null)
            {
                BindBranchList();
            }
        }
        protected void ddlArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCountry();
        }
        protected void rptRelatedImgList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal ltImg = e.Item.FindControl("ltImg") as Literal;
                M_MediaRoomRelatedDetail tempENT = e.Item.DataItem as M_MediaRoomRelatedDetail;
                string KeyValue = e.Item.DataItem as string;
                if (tempENT != null)
                {
                    string[] key = tempENT.KeyMediaRelatedUID.Split('_');
                    string From = key[1];
                    int MediaRoomKeyUID = Convert.ToInt32(key[0]);
                    string strImgUrl = string.Empty;
                    if (From == "app")
                    {
                        app_tblMediaRoom_Related app_related = new app_tblMediaRoom_Related();
                        app_related.AppMediaRoomRelatedUID = MediaRoomKeyUID;
                        app_related = app_relatedBLL.GetListByENT(app_related).FirstOrDefault();
                        if (app_related != null)
                        {
                            if (app_related.AppMediaRoomUID > 0)
                            {
                                app_tblMediaRoom app_room = new app_tblMediaRoom();
                                app_room.AppMediaRoomUID = (int)app_related.AppMediaRoomUID;
                                app_room = app_mediaBLL.GetListByENT(app_room).FirstOrDefault();
                                if (app_room != null)
                                {
                                    strImgUrl = "<img src=\"" + app_room.FilePath + app_room.FileName + "_150_150.jpg \"/>";
                                }
                            }
                            else if (app_related.tblMediaRoom != null)
                            {
                                var temp = app_related.tblMediaRoom;
                                strImgUrl = "<img src=\"" + temp.FilePath + temp.FileName + "_150_150.jpg \"/>";
                            }
                        }
                    }
                    else
                    {
                        tblMediaRoom_Related tbl_related = new tblMediaRoom_Related();
                        tbl_related.MediaRoomRelatedUID = MediaRoomKeyUID;
                        tbl_related = tbl_relatedBLL.GetListByENT(tbl_related).FirstOrDefault();
                        if (tbl_related != null && tbl_related.tblMediaRoom != null)
                        {
                            var temp = tbl_related.tblMediaRoom;
                            strImgUrl = "<img src=\"" + temp.FilePath + temp.FileName + "_150_150.jpg \"/>";
                        }

                    }
                    ltImg.Text = strImgUrl;
                }
            }
        }
        
        protected void lkbtnSave_Click(object sender, EventArgs e)
        {
            customerENT = GetCustomerFromSession();
            if (customerENT != null)
            {
                CustomerUID = customerENT.CustomerUID;
                string strctcuid = Request.QueryString[Constant.UrlParamName.custToContactPersonUID];
                type = Request.QueryString["type"];
                isfromApp = Request.QueryString[Constant.UrlParamName.IsFromApp];
                if (!string.IsNullOrEmpty(strctcuid) && int.TryParse(strctcuid, out ctuid) && ctuid > 0 && !string.IsNullOrEmpty(isfromApp))
                {
                    Update();
                    WriteToLog();
                }
                else
                {
                    Add();
                    WriteToLog();
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "cleartxt", "ClearText();", true);
                }
                PageIsPostBack();

            }
        }
        protected void Update()
        {
            string strIsFromApp = Request.QueryString[Constant.UrlParamName.IsFromApp];
            if (!string.IsNullOrEmpty(strIsFromApp))
            {
                if (strIsFromApp.ToLower().Trim() == "true")
                    UpdateApp();
                else
                    AddToAppFromTbl();
            }
            else
            { lblMeg.Text = ResourceManage.GetResourceString("msg200017"); }
        }
        protected void Add()
        {
            app_tblCust_Branch app_branch = new app_tblCust_Branch();
            app_branch.CustomerUID = CustomerUID;
            app_tblAddress app_address = new app_tblAddress();
            app_tblContactInfo app_info = new app_tblContactInfo();
            if (tbl_branchuid > 0)
            {
                tblCust_Branch cust_branch = new tblCust_Branch();
                app_branch.CustBranchUID = tbl_branchuid;
                cust_branch.CustBranchUID = tbl_branchuid;
                cust_branch.CustomerUID = CustomerUID;
                cust_branch.Deleted = 0;
                cust_branch = tbl_cust_branchBLL.GetEntity(cust_branch);
                if (cust_branch != null)
                {
                    if (cust_branch.AddressUID > 0)
                        app_address.AddressUID = (int)cust_branch.AddressUID;
                    if (cust_branch.ContactInfoUID > 0)
                        app_info.ContactInfoUID = (int)cust_branch.ContactInfoUID;
                }
                var temp = app_cust_branchBLL.GetEntity(app_branch, false);
                if (temp != null && ctuid > 0 && type != "add")
                {
                    UpdateApp();
                    return;
                }
            }
            app_branch = GetApp_cust_BranchENT(app_branch);
            app_address = GetApp_AddressENT(app_address);
            app_info = getApp_infoENT(app_info);

            List<app_tblMediaRoom_Related> ToAddedList = new List<app_tblMediaRoom_Related>();
            List<app_tblMediaRoom_Related> ToDeletedList = new List<app_tblMediaRoom_Related>();
            List<app_tblMediaRoom_Related> ToUpdatedList = new List<app_tblMediaRoom_Related>();
            TransactionOptions options = new TransactionOptions();
            options.Timeout = new TimeSpan(0, 5, 0);
            options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
            {
                app_branchuid = app_cust_branchBLL.InsertENT(app_branch);
                int app_addressuid= app_addressBLL.InsertENT(app_address);
                int app_infouid= app_infoBLL.InsertENT(app_info);
                app_branch.AppCustBranchUID = app_branchuid;
                app_branch.AppAddressUID = app_addressuid;
                app_branch.AppContactInfoUID = app_infouid;
                app_cust_branchBLL.UpdateENT(app_branch);
                DetchImg(ToAddedList, ToDeletedList, ToUpdatedList);
                app_relatedBLL.SaveList(ToAddedList, ToDeletedList, ToUpdatedList);
                try
                {
                    scope.Complete();
                    lblMeg.Text = ResourceManage.GetResourceString("msg200004");
                }
                catch (Exception er)
                { lblMeg.Text = ResourceManage.GetResourceString("msg200003"); }
            }
        }
        private void PageIsPostBack()
        {
            hidAddMediaRelated.Value= hidDeleteMediaRelated.Value = hidRelatedMediastrUID.Value = hidUpdateMediaRelated.Value = hidVideoValue.Value = "";
            BindBranchList();
            BindImgRelated(false);
        }

        private void UpdateApp()
        {
            #region 修改
            app_tblContactInfo app_info = new app_tblContactInfo();
            app_tblAddress app_address = new app_tblAddress();
            app_tblCust_Branch app_cust_branch = new app_tblCust_Branch();
            app_branchuid = app_cust_branch.AppCustBranchUID = ctuid;
            app_cust_branch.CustomerUID=CustomerUID;
            app_cust_branch.Deleted = 0;
            app_cust_branch = app_cust_branchBLL.GetEntity(app_cust_branch, true);
            if (app_cust_branch != null)
            {
                if (tbl_branchuid > 0)
                    app_cust_branch.CustBranchUID = tbl_branchuid;
                app_cust_branch.Deleted = 0;
                app_cust_branch = GetApp_cust_BranchENT(app_cust_branch);
                if (app_cust_branch.AppContactInfoUID > 0)
                {
                    app_info.AppContactInfoUID = (int)app_cust_branch.AppContactInfoUID;
                    app_info = app_infoBLL.GetAppENTByENT(app_info);
                    app_info = getApp_infoENT(app_info);
                }
                if (app_cust_branch.AppAddressUID > 0)
                {
                    app_address.AppAddressUID = (int)app_cust_branch.AppAddressUID;
                    app_address = app_addressBLL.GetAppENTByENT(app_address);
                    app_address = GetApp_AddressENT(app_address);
                }

                List<app_tblMediaRoom_Related> ToAddedList = new List<app_tblMediaRoom_Related>();
                List<app_tblMediaRoom_Related> ToDeletedList = new List<app_tblMediaRoom_Related>();
                List<app_tblMediaRoom_Related> ToUpdatedList = new List<app_tblMediaRoom_Related>();
                

                TransactionOptions options = new TransactionOptions();
                options.Timeout = new TimeSpan(0, 5, 0);
                options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
                {
                    app_infoBLL.UpdateENT(app_info);
                    app_addressBLL.UpdateENT(app_address);
                    app_cust_branchBLL.UpdateENT(app_cust_branch);
                    DetchImg(ToAddedList, ToDeletedList, ToUpdatedList);
                    app_relatedBLL.SaveList(ToAddedList, ToDeletedList, ToUpdatedList);
                    try
                    {
                        scope.Complete();
                        lblMeg.Text = ResourceManage.GetResourceString("msg200004");
                    }
                    catch (Exception er)
                    { lblMeg.Text = ResourceManage.GetResourceString("msg200003"); }
                }
            }
            #endregion
            else
            { lblMeg.Text = ResourceManage.GetResourceString("msg200014"); }
        }
        private void DetchImg(List<app_tblMediaRoom_Related> ToAddList,List<app_tblMediaRoom_Related> ToDeleted,List<app_tblMediaRoom_Related> ToUpdated)
        {
            string strAdd = hidAddMediaRelated.Value;
            string strDelete = hidDeleteMediaRelated.Value;
            string strUpdate = hidUpdateMediaRelated.Value;
            string[] strAddTemp = strAdd.Split(',');
            string[] strDeltedTemp = strDelete.Split(',');
            string[] strUpdateTemp = strUpdate.Split(',');
            string MediaFrom = "";
            string MediaRelatedFrom = "";
            int MediaRelatedKeyUID = 0;
            int MediaKeyUID = 0;
            #region 新增处理
            foreach (var temp in strAddTemp)
            {
                app_tblMediaRoom_Related tempENT = new app_tblMediaRoom_Related();
                if (!string.IsNullOrEmpty(temp))
                {
                    string[] info = temp.Split('_');
                    MediaFrom = info[1];
                    MediaKeyUID = Convert.ToInt32(info[0]);

                    if (MediaFrom == "app")
                    { tempENT.AppMediaRoomUID = MediaKeyUID; }
                    else if (MediaFrom == "tbl")
                    { tempENT.MediaRoomUID = MediaKeyUID; }
                    tempENT.AppRelatedUID = ctuid;
                    if (tbl_branchuid > 0)
                        tempENT.RelatedUID = tbl_branchuid;
                    if (app_branchuid > 0)
                        tempENT.AppRelatedUID = app_branchuid;
                    tempENT.Deleted = 0;
                    tempENT.IsPublish = 1;
                    tempENT.Category = Constant.MediaRoomCategory.branch_image;
                    ToAddList.Add(tempENT);
                }
            }
            #endregion
            # region 删除处理
            foreach (var temp in strDeltedTemp)
            {
                app_tblMediaRoom_Related app_RelatedENT = new app_tblMediaRoom_Related();
                if (!string.IsNullOrEmpty(temp))
                {
                    string[] info = temp.Split('_');
                    MediaRelatedFrom = info[1];
                    MediaRelatedKeyUID = Convert.ToInt32(info[0]);

                    string[] info2 = temp.Split('_');
                    MediaFrom = info2[1];
                    MediaKeyUID = Convert.ToInt32(info2[0]);
                    tblMediaRoom_Related tempRelatedENT = new tblMediaRoom_Related();
                    if (MediaRelatedFrom == "tbl")
                    {

                        tempRelatedENT.Deleted = 0;
                        tempRelatedENT.MediaRoomRelatedUID = MediaRelatedKeyUID;
                        tempRelatedENT.Category = Constant.MediaRoomCategory.branch_image;
                        tempRelatedENT.RelatedUID = tbl_branchuid;
                        tempRelatedENT = tbl_relatedBLL.GetEntity(tempRelatedENT);
                        if (tempRelatedENT != null)
                        {
                            app_RelatedENT.MediaRoomRelatedUID = tempRelatedENT.MediaRoomRelatedUID;
                            app_RelatedENT.AppRelatedUID = app_branchuid;
                            app_RelatedENT.RelatedUID = tbl_branchuid;
                            app_RelatedENT.Category = tempRelatedENT.Category;
                            app_RelatedENT.IsPublish = tempRelatedENT.IsPublish;
                            app_RelatedENT.Deleted = 1;
                            app_RelatedENT.MediaRoomUID = MediaKeyUID;
                            app_RelatedENT.Category = tempRelatedENT.Category;
                            ToAddList.Add(app_RelatedENT);
                        }
                    }
                    else
                    {
                        app_RelatedENT.AppMediaRoomRelatedUID = MediaRelatedKeyUID;
                        var tempList = app_relatedBLL.GetListByENT(app_RelatedENT, false);
                        if (tempList != null)
                        {
                            app_RelatedENT = tempList.FirstOrDefault();
                            if (app_RelatedENT.MediaRoomRelatedUID > 0)
                            {
                                app_RelatedENT.Deleted = 1;
                                ToUpdated.Add(app_RelatedENT);
                            }
                            else
                            { ToDeleted.Add(app_RelatedENT); }
                        }
                    }
                }
            }
            #endregion
            #region 修改操作
            foreach (var temp in strUpdateTemp)
            {
                if (!string.IsNullOrEmpty(temp))
                {
                    string[] strRelated = temp.Split('-');
                    string strRelatedinfo = strRelated[0];
                    string strMediaInfo = strRelated[1];

                    string[] relatedinfo = strRelatedinfo.Split('_');
                    string[] mediainfo = strMediaInfo.Split('_');
                    MediaFrom = mediainfo[1];
                    MediaKeyUID = Convert.ToInt32(mediainfo[0]);
                    MediaRelatedKeyUID = Convert.ToInt32(relatedinfo[0]);
                    MediaRelatedFrom = relatedinfo[1];
                    tblMediaRoom_Related tempRelatedENT = new tblMediaRoom_Related();
                    app_tblMediaRoom_Related app_RelatedENT = new app_tblMediaRoom_Related();
                    app_RelatedENT.MediaRoomRelatedUID = MediaRelatedKeyUID;
                    if (tbl_branchuid > 0)
                        app_RelatedENT.RelatedUID = tbl_branchuid;
                    app_RelatedENT.AppRelatedUID = app_branchuid;
                    app_RelatedENT.Category = Constant.MediaRoomCategory.branch_image;
                    var tempAppRelatedENT = app_relatedBLL.GetEntity(app_RelatedENT, false);
                    if (tempAppRelatedENT != null)
                    {
                        if (MediaFrom == "tbl")
                            tempAppRelatedENT.MediaRoomUID = MediaKeyUID;
                        else
                            tempAppRelatedENT.AppMediaRoomUID = MediaKeyUID;
                    }
                    if (MediaRelatedFrom == "tbl")
                    {
                        tempRelatedENT.MediaRoomRelatedUID = MediaRelatedKeyUID;
                        tempRelatedENT.Category = Constant.MediaRoomCategory.branch_image;
                        tempRelatedENT.RelatedUID = tbl_branchuid;
                        tempRelatedENT = tbl_relatedBLL.GetEntity(tempRelatedENT);
                        if (tempRelatedENT != null)
                        {
                            app_RelatedENT.MediaRoomRelatedUID = MediaRelatedKeyUID;
                            app_RelatedENT.AppRelatedUID = app_branchuid;
                            var tempENT = app_relatedBLL.GetEntity(app_RelatedENT, false);
                            if (tempENT != null)
                            {
                                if (MediaFrom == "tbl")
                                    tempENT.MediaRoomUID = MediaKeyUID;
                                else
                                    tempENT.AppMediaRoomUID = MediaKeyUID;
                                tempENT.Deleted = 0;
                                ToUpdated.Add(tempENT);
                            }
                            else
                            {
                                if (tbl_branchuid > 0)
                                    app_RelatedENT.RelatedUID = tbl_branchuid;
                                if (MediaFrom == "tbl")
                                    app_RelatedENT.MediaRoomUID = MediaKeyUID;
                                else
                                    app_RelatedENT.AppMediaRoomUID = MediaKeyUID;
                                app_RelatedENT.Deleted = 0;
                                app_RelatedENT.Category = Constant.MediaRoomCategory.branch_image;
                                app_RelatedENT.IsPublish = 1;
                                ToAddList.Add(app_RelatedENT);
                            }
                        }
                    }
                    else
                    {
                        if (MediaFrom == "tbl")
                        {
                            app_RelatedENT.MediaRoomUID = MediaKeyUID;
                            app_RelatedENT.AppMediaRoomUID = null;
                        }
                        else
                        {
                            app_RelatedENT.MediaRoomUID = null;
                            app_RelatedENT.AppMediaRoomUID = MediaKeyUID;
                        }
                        ToUpdated.Add(app_RelatedENT);
                    }
                }
            }
            #endregion
        }
        
        private app_tblCust_Branch GetApp_cust_BranchENT(app_tblCust_Branch ent)
        {
            ent.BranchName_Chi = txtBranchName_Chi.Text;
            ent.BranchName_Eng = txtBranchName_Eng.Text;
            int TempNo=0;
            int.TryParse(txtNoOfEmployee.Text,out TempNo);
            if (TempNo > 0)
                ent.NOfEmp = TempNo;
            int TempSeqNo = 0;
            int.TryParse(txtSequenceNo.Text,out TempSeqNo);
            if (TempSeqNo > 0)
                ent.SequenceNo = TempSeqNo;
            return ent;
        }
        private app_tblContactInfo getApp_infoENT(app_tblContactInfo infoENT)
        {
            #region Email
            infoENT.Email1 = TextBoxEmail1.Text;
            infoENT.Email1_IsSubscribe = CheckBoxES1.Checked ? 1 : 0;
            infoENT.Email1_Type = Convert.ToInt32(DropDownListEm1.SelectedValue);
            infoENT.Email1_HardBounce = CheckBoxHB1.Checked ? 1 : 0;
            infoENT.Email2 = TextBoxEmail2.Text;
            infoENT.Email2_IsSubscribe = CheckBoxES2.Checked ? 1 : 0;
            infoENT.Email2_Type = Convert.ToInt32(DropDownListEm2.SelectedValue);
            infoENT.Email2_HardBounce = CheckBoxHB2.Checked ? 1 : 0;
            infoENT.Email3 = TextBoxEmail3.Text;
            infoENT.Email3_IsSubscribe = CheckBoxES3.Checked ? 1 : 0;
            infoENT.Email3_Type = Convert.ToInt32(DropDownListEm3.SelectedValue);
            infoENT.Email3_HardBounce = CheckBoxHB3.Checked ? 1 : 0;
            infoENT.Email4 = TextBoxEmail4.Text;
            infoENT.Email4_IsSubscribe = CheckBoxES4.Checked ? 1 : 0;
            infoENT.Email4_Type = Convert.ToInt32(DropDownListEm4.SelectedValue);
            infoENT.Email4_HardBounce = CheckBoxHB4.Checked ? 1 : 0;
            infoENT.Email5 = TextBoxEmail5.Text;
            infoENT.Email5_IsSubscribe = CheckBoxES5.Checked ? 1 : 0;
            infoENT.Email5_Type = Convert.ToInt32(DropDownListEm5.SelectedValue);
            infoENT.Email5_HardBounce = CheckBoxHB5.Checked ? 1 : 0;
            #endregion
            #region Fax
            infoENT.Fax1 = TextBoxF1.Text;
            infoENT.Fax1_AreaCode = TextBoxFA1.Text;
            infoENT.Fax1_CountryCode = TextBoxFC1.Text;
            infoENT.Fax1_IsDNC = CheckBoxFD1.Checked ? 1 : 0;
            infoENT.Fax1_IsSubscribe = CheckBoxFS1.Checked ? 1 : 0;
            infoENT.Fax1_Type = Convert.ToInt32(DropDownListFax1.SelectedValue);

            infoENT.Fax2 = TextBoxF2.Text;
            infoENT.Fax2_AreaCode = TextBoxFA2.Text;
            infoENT.Fax2_CountryCode = TextBoxFC2.Text;
            infoENT.Fax2_IsDNC = CheckBoxFD2.Checked ? 1 : 0;
            infoENT.Fax2_IsSubscribe = CheckBoxFS2.Checked ? 1 : 0;
            infoENT.Fax2_Type = Convert.ToInt32(DropDownListFax2.SelectedValue);

            infoENT.Fax3 = TextBoxF3.Text;
            infoENT.Fax3_AreaCode = TextBoxFA3.Text;
            infoENT.Fax3_CountryCode = TextBoxFC3.Text;
            infoENT.Fax3_IsDNC = CheckBoxFD3.Checked ? 1 : 0;
            infoENT.Fax3_IsSubscribe = CheckBoxFS3.Checked ? 1 : 0;
            infoENT.Fax3_Type = Convert.ToInt32(DropDownListFax3.SelectedValue);

            infoENT.Fax4 = TextBoxF4.Text;
            infoENT.Fax4_AreaCode = TextBoxFA4.Text;
            infoENT.Fax4_CountryCode = TextBoxFC4.Text;
            infoENT.Fax4_IsDNC = CheckBoxFD4.Checked ? 1 : 0;
            infoENT.Fax4_IsSubscribe = CheckBoxFS4.Checked ? 1 : 0;
            infoENT.Fax4_Type = Convert.ToInt32(DropDownListFax4.SelectedValue);

            infoENT.Fax5 = TextBoxF5.Text;
            infoENT.Fax5_AreaCode = TextBoxFA5.Text;
            infoENT.Fax5_CountryCode = TextBoxFC5.Text;
            infoENT.Fax5_IsDNC = CheckBoxFD5.Checked ? 1 : 0;
            infoENT.Fax5_IsSubscribe = CheckBoxFS5.Checked ? 1 : 0;
            infoENT.Fax5_Type = Convert.ToInt32(DropDownListFax5.SelectedValue);
            #endregion
            #region Tel
            infoENT.Tel1 = TextBoxT1.Text;
            infoENT.Tel1_AreaCode = TextBoxTA1.Text;
            infoENT.Tel1_CountryCode = TextBoxTC1.Text;
            infoENT.Tel1_IsDNC = CheckBoxTD1.Checked ? 1 : 0;
            infoENT.Tel1_IsSubscribe = CheckBoxTS1.Checked ? 1 : 0;
            infoENT.Tel1_Type = Convert.ToInt32(DropDownListTel1.SelectedValue);

            infoENT.Tel2 = TextBoxT2.Text;
            infoENT.Tel2_AreaCode = TextBoxTA2.Text;
            infoENT.Tel2_CountryCode = TextBoxTC2.Text;
            infoENT.Tel2_IsDNC = CheckBoxTD2.Checked ? 1 : 0;
            infoENT.Tel2_IsSubscribe = CheckBoxTS2.Checked ? 1 : 0;
            infoENT.Tel2_Type = Convert.ToInt32(DropDownListTel2.SelectedValue);

            infoENT.Tel3 = TextBoxT3.Text;
            infoENT.Tel3_AreaCode = TextBoxTA3.Text;
            infoENT.Tel3_CountryCode = TextBoxTC3.Text;
            infoENT.Tel3_IsDNC = CheckBoxTD3.Checked ? 1 : 0;
            infoENT.Tel3_IsSubscribe = CheckBoxTS3.Checked ? 1 : 0;
            infoENT.Tel3_Type = Convert.ToInt32(DropDownListTel3.SelectedValue);

            infoENT.Tel4 = TextBoxT4.Text;
            infoENT.Tel4_AreaCode = TextBoxTA4.Text;
            infoENT.Tel4_CountryCode = TextBoxTC4.Text;
            infoENT.Tel4_IsDNC = CheckBoxTD4.Checked ? 1 : 0;
            infoENT.Tel4_IsSubscribe = CheckBoxTS4.Checked ? 1 : 0;
            infoENT.Tel4_Type = Convert.ToInt32(DropDownListTel4.SelectedValue);

            infoENT.Tel5 = TextBoxT5.Text;
            infoENT.Tel5_AreaCode = TextBoxTA5.Text;
            infoENT.Tel5_CountryCode = TextBoxTC5.Text;
            infoENT.Tel5_IsDNC = CheckBoxTD5.Checked ? 1 : 0;
            infoENT.Tel5_IsSubscribe = CheckBoxTS5.Checked ? 1 : 0;
            infoENT.Tel5_Type = Convert.ToInt32(DropDownListTel5.SelectedValue);
            #endregion

            #region WebSite
            infoENT.Website1 = TextBoxWeb1.Text;
            infoENT.Website1_Type = Convert.ToInt32(DropDownListW1.SelectedValue);
            infoENT.Website2 = TextBoxWeb2.Text;
            infoENT.Website2_Type = Convert.ToInt32(DropDownListW2.SelectedValue);
            #endregion
            return infoENT;
        }
        private app_tblAddress GetApp_AddressENT(app_tblAddress addressENT)
        {
            addressENT.Area_Chi = txtArea_Chi.Text;
            addressENT.Area_Eng = txtArea_Eng.Text;
            addressENT.City_Chi = txtCity_Chi.Text;
            addressENT.City_Eng = txtCity_Eng.Text;
            addressENT.CountryUID = Convert.ToInt32(ddlCountry.SelectedValue);
            addressENT.CreationDate = DateTime.Now;
            addressENT.District_Chi = txtDistrict_Chi.Text;
            addressENT.District_Eng = txtDistrict_Eng.Text;
            addressENT.Flat_Bldg_Chi = txtFlat_Chi.Text;
            addressENT.Flat_Bldg_Eng = txtFlat_Eng.Text;
            addressENT.LastModifyBy = Common.GetManageName();
            addressENT.LastModifyDate = DateTime.Now;
            addressENT.PostalCode = txtPostCode.Text;
            addressENT.Province_Chi = txtProvince_Chi.Text;
            addressENT.Province_Eng = txtProvince_Eng.Text;
            addressENT.Street_Rd_Chi = txtStreet_Chi.Text;
            addressENT.Street_Rd_Eng = txtStreet_Eng.Text;
            mtblCountry countryENT = new mtblCountry();
            countryENT = addressENT.mtblCountry;
            StringBuilder strBuilderEng = new StringBuilder();
            strBuilderEng.Append(txtFlat_Eng.Text).Append(txtStreet_Eng.Text).Append(txtArea_Eng.Text).Append(txtDistrict_Eng.Text).Append(txtCity_Eng.Text).Append(txtProvince_Eng.Text);
            StringBuilder strBuilderChi = new StringBuilder();
            if (countryENT != null)
            {
                strBuilderEng.Append(countryENT.Country_Eng);

                if (countryENT.AreaUID > 0)
                {
                    mtblArea areaENT = new mtblArea();
                    areaENT.AreaUID = (int)countryENT.AreaUID;
                    mtblAreaBLL bll = new mtblAreaBLL();
                    areaENT = bll.GetEntity(areaENT);
                    if (areaENT != null)
                    {
                        strBuilderEng.Append(areaENT.Area_Eng);
                        strBuilderChi.Append(areaENT.Area_Chi);
                    }
                }
                strBuilderChi.Append(countryENT.Country_Chi);
            }
            strBuilderChi.Append(txtProvince_Chi.Text).Append(txtCity_Chi.Text).Append(txtDistrict_Chi.Text).Append(txtArea_Chi.Text).Append(txtStreet_Chi.Text).Append(txtFlat_Chi.Text);
            addressENT.FullAdd_Chi = strBuilderChi.ToString();
            addressENT.FullAdd_Eng = strBuilderEng.ToString();
            return addressENT;
        }
        private void AddToAppFromTbl()
        {
            tblCust_Branch cust_branch=new tblCust_Branch();
            cust_branch.CustomerUID = CustomerUID;
            tbl_branchuid= cust_branch.CustBranchUID = ctuid;
            cust_branch.Deleted = 0;
            cust_branch = tbl_cust_branchBLL.GetEntity(cust_branch);
            if (cust_branch == null)
            {
                lblMeg.Text = ResourceManage.GetResourceString("msg200013");
                return;
            }
            else
            {
                app_tblCust_Branch app_cust_branch=new app_tblCust_Branch();
                app_cust_branch.CustBranchUID = ctuid;
                app_cust_branch.CustomerUID = CustomerUID;
                var tempENT = app_cust_branchBLL.GetEntity(app_cust_branch, false);
                if (tempENT == null)
                {
                    Add();
                }
                else
                {
                    app_branchuid = app_cust_branch.AppCustBranchUID;
                    if (app_cust_branch.CustBranchUID > 0)
                        tbl_branchuid = (int)app_cust_branch.CustBranchUID;
                    if (tempENT.Deleted == 1)
                    {
                        lblMeg.Text = ResourceManage.GetResourceString("msg200014");
                        return;
                    }
                    else
                    {
                        app_branchuid= ctuid = tempENT.AppCustBranchUID;
                        UpdateApp();
                    }
                }
            }
        }

        #region
        protected string MediaRelatedChange(string info)
        {
            List<int> oldList = new List<int>();
            List<int> newList = new List<int>();
            string[] Str = MedioRelated.Split(',');
            string[] tempStr = hidAddMediaRelated.Value.Split(',');
            for (int i = 0; i < Str.Length - 1; i++)
            {

                int temp = Str[i].Equals("") ? 0 : Convert.ToInt32(Str[i]);
                if (temp > 0)
                    oldList.Add(temp);
            }
            for (int i = 0; i < tempStr.Length - 1; i++)
            {
                int temp = tempStr[i].Equals("") ? 0 : Convert.ToInt32(tempStr[i]);
                if (temp > 0)
                    newList.Add(temp);
            }
            List<int> temp_a = GetChangeList(oldList, newList);
            List<int> temp_b = GetChangeList(newList, oldList);
            if (temp_a.Count > 0 || temp_b.Count > 0)
            {
                info += "MediaRelated have change";
            }
            return info;
        }
        private string GetVideoValueChange(string info)
        {
            string temp = hidVideoValue.Value;
            if (vidovalue.Trim() != temp.Trim())
            {
                info = "Video have change";
            }
            return info;
        }
        private List<int> GetChangeList(List<int> Contrast, List<int> Source)
        {
            List<int> add = new List<int>();
            for (int i = 0; i < Contrast.Count(); i++)
            {
                bool has = Source.Contains(Contrast[i]);
                if (has == false)
                    add.Add(Contrast[i]);
            }
            return add;
        }
        protected void WriteStaticEnt()
        {
            staticAppCustBranch = new app_tblCust_Branch();
            staticAppContactInfo = new app_tblContactInfo();
            staticAppAddress = new app_tblAddress();
            staticAppCustBranch = GetApp_cust_BranchENT(staticAppCustBranch);
            staticAppContactInfo = getApp_infoENT(staticAppContactInfo);
            staticAppAddress = GetApp_AddressENT(staticAppAddress);
            vidovalue = hidVideoValue.Value;
            MedioRelated = hidAddMediaRelated.Value;
        }
        protected void WriteToLog()
        {
            BasePage basepage = new BasePage();
            string ChangeLoge = string.Empty;
            string LoginName = basepage.GetManageFromSession().User_Name;
            #region 获取最新的数据判断更改了什么
            ChangeLoge += GetAddressChange(ChangeLoge);
            ChangeLoge += MediaRelatedChange(ChangeLoge);
            ChangeLoge += GetVideoValueChange(ChangeLoge);
            ChangeLoge += GetInfoChange(ChangeLoge);
            #endregion
            Literal CompanyName = usermeg.FindControl("ltlCompanyName_eng") as Literal;
            tblHistory entlog = new tblHistory();
            entlog.HistoryCatUID = 2;
            entlog.HistoryTypeUID = 6;
            entlog.CustomerUID = Convert.ToInt32(Request.QueryString[Constant.UrlParamName.CustomerUID]);
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = LoginName + " {" + CompanyName.Text + "}{BranchOffice} " + ChangeLoge;
            entlog.LastModifyBy = LoginName;
            int tal = tblHistoryBLL.GetInstance().InsertENT(entlog);
        }
        protected string GetAddressChange(string ChangeLoge) {
            if (Val(staticAppCustBranch.SequenceNo.ToString()) != txtSequenceNo.Text) { ChangeLoge += ", SequenceNo change to" + txtSequenceNo.Text; }
            if (Val(staticAppCustBranch.BranchName_Eng) != txtBranchName_Eng.Text) { ChangeLoge += ", BranchName change to" + txtBranchName_Eng.Text; }
            if (Val(staticAppCustBranch.BranchName_Chi) != txtBranchName_Chi.Text) { ChangeLoge += ", 分行名稱 change to" + txtBranchName_Chi.Text; }
            if (Val(staticAppCustBranch.NOfEmp.ToString()) != txtNoOfEmployee.Text) { ChangeLoge += ", 	No. of Employee change to" + txtNoOfEmployee.Text; }
            if (Val(staticAppAddress.Area_Chi) != txtArea_Chi.Text) { ChangeLoge += ", 地區 change to " + txtArea_Chi.Text; }
            if (Val(staticAppAddress.Area_Eng) != txtArea_Eng.Text) { ChangeLoge += " , Area change to " + txtArea_Eng.Text; }
            if (Val(staticAppAddress.City_Chi) != txtCity_Chi.Text) { ChangeLoge += " , 城市 change to " + txtCity_Chi.Text; }
            if (Val(staticAppAddress.City_Eng) != txtCity_Eng.Text) { ChangeLoge += " , City change to " + txtCity_Eng.Text; }
            if (Val(staticAppAddress.District_Chi) != txtDistrict_Chi.Text) { ChangeLoge += " , 區域 change to " + txtDistrict_Chi.Text; }
            if (Val(staticAppAddress.District_Eng) != txtDistrict_Eng.Text) { ChangeLoge += " , District change to " + txtDistrict_Eng.Text; }
            if (Val(staticAppAddress.Flat_Bldg_Eng) != txtFlat_Eng.Text) { ChangeLoge += " , 室, 樓層, 大廈 change to " + txtFlat_Eng.Text; }
            if (Val(staticAppAddress.Flat_Bldg_Chi) != txtFlat_Chi.Text) { ChangeLoge += " , Flat_Bldg change to " + txtFlat_Chi.Text; }
            if (Val(staticAppAddress.Province_Chi) != txtProvince_Chi.Text) { ChangeLoge += " , 州, 省 change to " + txtProvince_Chi.Text; }
            if (Val(staticAppAddress.Province_Eng) != txtProvince_Eng.Text) { ChangeLoge += " , Province change to " + txtProvince_Eng.Text; }
            if (Val(staticAppAddress.Street_Rd_Chi) != txtStreet_Chi.Text) { ChangeLoge += " , 街道 change to " + txtStreet_Chi.Text; }
            if (Val(staticAppAddress.Street_Rd_Eng) != txtStreet_Eng.Text) { ChangeLoge += " , Street_Rd change to" + txtStreet_Eng.Text; }
            if (Val(staticAppAddress.PostalCode) != txtPostCode.Text) { ChangeLoge += " , PostalCode change to " + txtPostCode.Text; }
            if (Val(staticAppAddress.Department_Eng) != ddlArea.SelectedItem.Text) { ChangeLoge += " , Continent change to " + ddlArea.SelectedItem.Text; }
            if (Val(staticAppAddress.Department_Chi) != ddlCountry.SelectedItem.Text) { ChangeLoge += " , Country change to" + ddlCountry.SelectedItem.Text; }
            return ChangeLoge;
        }
        protected string GetInfoChange(string str)
        {
            if (staticAppContactInfo != null)
            {
                app_tblContactInfo infoENT = new app_tblContactInfo();
                infoENT = getApp_infoENT(infoENT);

                if (Val(staticAppContactInfo.Tel1) != Val(infoENT.Tel1) | Val(staticAppContactInfo.Tel2) != Val(infoENT.Tel2) | Val(staticAppContactInfo.Tel3) != Val(infoENT.Tel3) | Val(staticAppContactInfo.Tel4) != Val(infoENT.Tel4) | Val(staticAppContactInfo.Tel5) != Val(infoENT.Tel5))
                {
                    str += "Tel have change; ";
                }
                if (staticAppContactInfo.Tel1_IsDNC != infoENT.Tel1_IsDNC | staticAppContactInfo.Tel2_IsDNC != infoENT.Tel2_IsDNC | staticAppContactInfo.Tel3_IsDNC != infoENT.Tel3_IsDNC | staticAppContactInfo.Tel4_IsDNC != infoENT.Tel4_IsDNC | staticAppContactInfo.Tel5_IsDNC != infoENT.Tel5_IsDNC)
                {
                    str += "Tel DNC have change; ";
                }
                if (staticAppContactInfo.Tel1_IsSubscribe != infoENT.Tel1_IsSubscribe | staticAppContactInfo.Tel2_IsSubscribe != infoENT.Tel2_IsSubscribe | staticAppContactInfo.Tel3_IsSubscribe != infoENT.Tel3_IsSubscribe | staticAppContactInfo.Tel4_IsSubscribe != infoENT.Tel4_IsSubscribe | staticAppContactInfo.Tel5_IsSubscribe != infoENT.Tel5_IsSubscribe)
                {
                    str += "Tel Subscrib have change; ";
                }
                if (Val(staticAppContactInfo.Fax1) != Val(infoENT.Fax1) | Val(staticAppContactInfo.Fax2) != Val(infoENT.Fax2) | Val(staticAppContactInfo.Fax3) != Val(infoENT.Fax3) | Val(staticAppContactInfo.Fax4) != Val(infoENT.Fax4) | Val(staticAppContactInfo.Fax5) != Val(infoENT.Fax5))
                {
                    str += "Fax have change";
                }
                if (staticAppContactInfo.Fax1_IsDNC != infoENT.Fax1_IsDNC | staticAppContactInfo.Fax2_IsDNC != infoENT.Fax2_IsDNC | staticAppContactInfo.Fax3_IsDNC != infoENT.Fax3_IsDNC | staticAppContactInfo.Fax4_IsDNC != infoENT.Fax4_IsDNC | staticAppContactInfo.Fax5_IsDNC != infoENT.Fax5_IsDNC)
                {
                    str += "Fax DNC chave change";
                }
                if (staticAppContactInfo.Fax1_IsSubscribe != infoENT.Fax1_IsSubscribe | staticAppContactInfo.Fax2_IsSubscribe != infoENT.Fax2_IsSubscribe | staticAppContactInfo.Fax3_IsSubscribe != infoENT.Fax3_IsSubscribe | staticAppContactInfo.Fax4_IsSubscribe != infoENT.Fax4_IsSubscribe | staticAppContactInfo.Fax5_IsSubscribe != infoENT.Fax5_IsSubscribe)
                {
                    str += "Fax Subscribe have change";
                }
                if (Val(staticAppContactInfo.Email1) != Val(infoENT.Email1) | Val(staticAppContactInfo.Email2) != Val(infoENT.Email2) | Val(staticAppContactInfo.Email3) != Val(infoENT.Email3) | Val(staticAppContactInfo.Email4) != Val(infoENT.Email4) | Val(staticAppContactInfo.Email5) != Val(infoENT.Email5))
                {
                    str += "Email have change";
                }
                if (staticAppContactInfo.Email1_IsSubscribe != infoENT.Email1_IsSubscribe | staticAppContactInfo.Email2_IsSubscribe != infoENT.Email2_IsSubscribe | staticAppContactInfo.Email3_IsSubscribe != infoENT.Email3_IsSubscribe | staticAppContactInfo.Email4_IsSubscribe != infoENT.Email4_IsSubscribe | staticAppContactInfo.Email5_IsSubscribe != infoENT.Email5_IsSubscribe)
                {
                    str += "Email Subscribe have change";
                }
                if (staticAppContactInfo.Email1_HardBounce != infoENT.Email1_HardBounce | staticAppContactInfo.Email2_HardBounce != infoENT.Email2_HardBounce | staticAppContactInfo.Email3_HardBounce != infoENT.Email3_HardBounce | staticAppContactInfo.Email4_HardBounce != infoENT.Email4_HardBounce | staticAppContactInfo.Email5_HardBounce != infoENT.Email5_HardBounce)
                {
                    str += "Email Subscribe have change";
                }
                if (staticAppContactInfo.Website1 != infoENT.Website1 | staticAppContactInfo.Website2 != infoENT.Website2 | staticAppContactInfo.Website3 != infoENT.Website3 | staticAppContactInfo.Website4 != infoENT.Website4 | staticAppContactInfo.Website5 != infoENT.Website5)
                {
                    str += "Website have change";
                }
                if (staticAppContactInfo.Website1_Type != infoENT.Website1_Type | staticAppContactInfo.Website2_Type != infoENT.Website2_Type)
                {
                    str += "Website type have change";
                }
            }
            return str;
        }

        protected string Val(string str)
        {
            if (string.IsNullOrEmpty(str))
                str = "";
            return str;
        }
        #endregion
    }
}